package com.scedu.controller;

import com.scedu.domain.ProductCategory;
import com.scedu.domain.ProductInfo;
import com.scedu.exception.SellException;
import com.scedu.form.CategoryForm;
import com.scedu.form.ProductForm;
import com.scedu.service.ProductCategoryService;
import com.scedu.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
卖家类目
 */
@Slf4j
@Controller
@RequestMapping("/seller/category")
public class SellerCategoryController {
    @Autowired
    ProductCategoryService productCategoryService;
    /*
  卖家端商品列表
   */
    @GetMapping("/list")
    public ModelAndView list(Map<String,Object> map){

        List<ProductCategory> categoryList= productCategoryService.findAll();

        map.put("categoryList",categoryList);
        return  new ModelAndView("category/list",map);
    }
    /*
  详情
   */
    @GetMapping("/index")
    public ModelAndView index (@RequestParam(value = "categoryId",required = false) Integer categoryId,
                               Map<String,Object> map){



        if (!(categoryId==null)) {
            try {

                ProductCategory productCategory = productCategoryService.findOne(categoryId);
                log.info("商品类型={}",productCategory);
                map.put("category", productCategory);

            } catch (SellException e) {
                log.error("商品类型不存在,productId={}", categoryId);
                map.put("msg", e.getMessage());
                map.put("url", "/sell/seller/category/list");
                return new ModelAndView("common/error", map);
            }
        }else{
            ProductCategory productCategory =new ProductCategory();
            map.put("category", productCategory);
        }
        return new ModelAndView("/category/index",map);
    }
    /*
 修改
  */
    @PostMapping("/save")
    public ModelAndView save (@Valid CategoryForm categoryForm, BindingResult bindingresult,
                              Map<String,Object> map){
        if (bindingresult.hasErrors()){
            //有验证错误
            log.error("表单创建错误 ，参数错误 productInfo={}" ,categoryForm);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            map.put("msg",  bindingresult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/category/index");
            return new ModelAndView("common/error", map);

            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }
        ProductCategory productCategory=new ProductCategory();
        try {
            //根据productId判断是否是新增
            if (categoryForm.getCategoryId()==null){
                //新商品，生成ID
                //categoryForm.(KeyUtil.genUniqueKey());
            }else{
                productCategory= productCategoryService.findOne(categoryForm.getCategoryId());
            }

            BeanUtils.copyProperties(categoryForm,productCategory);
            productCategoryService.save(productCategory);
        }catch (SellException e){
            log.error("商品类型保存错误,productForm={}", categoryForm);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/category/index");
            return new ModelAndView("common/error", map);
        }

        map.put("url","/sell/seller/category/list");
        return new ModelAndView("common/success",map);

    }
}
